package cn.bytecollege.service;

import cn.bytecollege.mapper.OrderDetailMapper;
import cn.bytecollege.mapper.OrderMapper;
import cn.bytecollege.mapper.UserAddressMapper;
import cn.bytecollege.model.OrderDetail;
import cn.bytecollege.model.Orders;
import cn.bytecollege.model.User;
import cn.bytecollege.model.UserAddress;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrdersService {
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;
    public Double getTotalPrice(Integer ordersId) {
        Double totalPrice = 0.0;
        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<OrderDetail>().eq("order_id", ordersId).select("goods_price", "goods_number");

        List<OrderDetail> orderDetails = orderDetailMapper.selectList(queryWrapper);

        for (OrderDetail orderDetail : orderDetails) {

            totalPrice += orderDetail.getGoodsPrice() * orderDetail.getGoodsNumber();
        }
        return totalPrice;
    }

    public Integer payNow(Integer ordersId) {
        Orders orders = Orders.builder().orderId(ordersId).orderState("待发货").build();
        orderMapper.updateById(orders);
        return 1;
    }

    public List<UserAddress> getAllAddress(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        List<UserAddress> userAddresses = userAddressMapper.selectByMap(map);
        return userAddresses;

    }
}
